function [model]=E_step(model, data, MAXESTEPITER, MaxFun, maxvalue, countVEM)

% E step of BC3E

MINDIFF   = 0.005;
conv_diff = 100000000;
count     = 0;
N         = model.N;

value0    = maxvalue;

while(conv_diff>MINDIFF && count<MAXESTEPITER)
    
    disp('count from E-step');
    count = count+1
    n     = 1;
    while (n<=N)
        
        model=update_phi(model,data,n);
%         value1 = cal_likelihood(model, data);
%         
%         if (compareval(value1, maxvalue))
%             maxvalue = value1;
%             %disp('correct');
%         else
%             disp('Incorrect');
%             keyboard;
%         end
        
        
        model=update_alpha_1(model,n, MaxFun, data);
%         value2 = cal_likelihood(model, data);
%         
%         
%         if (compareval(value2, maxvalue))
%             maxvalue = value2;
%             %disp('correct');
%         else
%             disp('Incorrect');
%             keyboard;
%         end
        
        
         
        model=update_alpha_2(model,n, MaxFun);
%         value3 = cal_likelihood(model, data);
%         
%         if (compareval(value3, maxvalue))
%             maxvalue = value3;
%             %disp('correct');
%         else
%             disp('Incorrect');
%             keyboard;
%         end
        
        %model=update_sigma_1(model,n, MaxFun);
%         value4 = cal_likelihood(model, data);
%         
%         if (compareval(value4, maxvalue))
%             maxvalue = value4;
%             %disp('correct');
%         else
%             disp('Incorrect');
%             keyboard;
%         end
        
        
        %model=update_sigma_2(model,n, MaxFun);
%         value5 = cal_likelihood(model, data);
%         
%         if (compareval(value5, maxvalue))
%             maxvalue = value5;
%             %disp('correct');
%         else
%             disp('Incorrect');
%             keyboard;
%         end
        
        n = n+1;
    end
    
    model=update_zeta_1(model);
%     value6 = cal_likelihood(model, data);
%     
%     if (compareval(value6, maxvalue))
%         maxvalue = value6;
%         %disp('correct');
%     else
%         disp('Incorrect');
%         keyboard;
%     end
    
    model=update_zeta_2(model);
    value7 = cal_likelihood(model, data);
    
    if (compareval(value7, maxvalue))
        maxvalue = value7;
        %disp('correct');
    else
        disp('Incorrect');
        keyboard;
    end
    
    conv_diff=abs(value0-value7)/abs(value0);
    value0=value7;
    
end

end

